/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javalens.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javalens.astronomy.LightCurve;

/**
 *
 * @author vermaak
 */
public class OutputCSV implements Output {

    private File outputFile;
    private LightCurve curve;

    public OutputCSV(File outputFile, LightCurve curve) {
        this.outputFile = outputFile;
        this.curve = curve;
    }

    public void save() throws IOException {
        double[] x = curve.getDays();
        double[] y = curve.getMags();
        double[] err = curve.getErrors();
        FileWriter fileWriter = new FileWriter(outputFile);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        for (int i = 0; i < x.length; ++i) {
            String errterm = err == null ? "" : "," + Double.toString(err[i]);
            bufferedWriter.write(x[i] + "," + y[i] + errterm + "\n");
        }
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}
